//
//  MJKTitleView.m
//  MJKTitleView
//
//  Created by mac on 16/6/21.
//  Copyright © 2016年 马俊凯. All rights reserved.
//

//常态时字体颜色
#define Normal_Color [UIColor blackColor]
//选中状态的颜色
#define Select_Color [UIColor blueColor]

#import "MJKTitleView.h"

@interface MJKTitleView()

@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation MJKTitleView
{
    UIScrollView *_scrollView;
    //scrollView的contentSize
    CGSize _size;
    float _orginX;
}
-(instancetype)initWithFrame:(CGRect)frame andItemNames:(NSArray *)titles{
    self = [super initWithFrame:frame];
    if (self) {
        //初始化子视图
        [self layoutWithTitles:titles];
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}
//初始化子视图
-(void)layoutWithTitles:(NSArray *)titles{
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.backgroundColor = [UIColor redColor];
    _orginX = 0;
    for (NSString *title in titles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:Normal_Color forState:UIControlStateNormal];
        [btn setTitleColor:Select_Color forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:20];
        //计算文字宽度
        float width = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 100) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size.width+20;
        //设置button的frame
        btn.frame = CGRectMake(_orginX, 0, width, CGRectGetHeight(self.frame));
        //添加到滚动视图上
        [_scrollView addSubview:btn];
        if (_orginX == 0) {
            //默认选中第一个
            btn.selected = YES;
            self.lineView.frame = CGRectMake(0, btn.frame.size.height-2, btn.frame.size.width, 2);
            self.lineView.backgroundColor = [UIColor blueColor];
        }
        //给button添加事件
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //添加到数组中
        [self.buttons addObject:btn];
        _orginX += width;
    }
    //设置scrollView
    _size = CGSizeMake(_orginX, self.bounds.size.height);
    _scrollView.contentSize = _size;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    //添加lineView
    [_scrollView addSubview:_lineView];
    //添加scrollView到self上
    [self addSubview:_scrollView];
}
-(void)btnClickWithBlock:(ClickBlock)block{
    self.clickItem = block;
}
#pragma mark - 点击事件
-(void)btnClick:(UIButton *)button{
    for (UIButton *btn in _buttons) {
        //取消选中所有button
        btn.selected = NO;
        btn.backgroundColor = [UIColor whiteColor];
    }
    //将当前button设为选中状态
    button.selected = YES;

    //动画的形式修改位置
    [UIView animateWithDuration:0.3 animations:^{
        if (button.center.x < self.center.x) {
            _scrollView.contentOffset = CGPointMake(0, _scrollView.frame.origin.y);
        }else if (button.center.x > self.center.x && button.center.x < _orginX-self.center.x){
            _scrollView.contentOffset = CGPointMake(button.center.x-self.center.x, _scrollView.frame.origin.y);
        }else{
            _scrollView.contentOffset = CGPointMake(_orginX - 2 * self.center.x, _scrollView.frame.origin.y);
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.lineView.frame = CGRectMake(button.frame.origin.x, _lineView.frame.origin.y, button.frame.size.width, _lineView.frame.size.height);
        }];
    }];
    NSLog(@"======%@======",NSStringFromCGPoint(_scrollView.contentOffset));
    if (self.clickItem) {
        self.clickItem([self.buttons indexOfObject:button]);
    }
}
#pragma mark - 懒加载
-(NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [[NSMutableArray alloc] init];
    }
    return _buttons;
}
-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
    }
    return _lineView;
}
@end





